<?php
/**
 * Description: 获取每日一题科目信息
 * User: devforma
 * Date: 16/3/4
 * Time: 17:36
 */

class GetTopicCategory implements iController {

    /**
     * api请求处理主函数
     * @param array $queryArgs 就是$_REQUEST变量
     * @return array api返回结果，必须含response_code
     */
    public function output($queryArgs) {
        global $cmsdb;

        $cats = array(
            1462 => '言语理解与表达',
            1461 => '判断推理',
            1460 => '数量关系',
            1459 => '常识判断',
            1458 => '资料分析'
        );

        $sql = 'SELECT t.name, tt.term_taxonomy_id FROM `wp_term_taxonomy` AS tt INNER JOIN `wp_terms` AS t
        ON tt.term_id=t.term_id AND tt.taxonomy="cats" AND tt.parent!=0';
        $cats = $cmsdb->getResults($sql);

        $category = array();
        foreach ($cats as $cat) {
            $category[$cat->term_taxonomy_id] = $cat->name;
        }

        $catStr = implode(',', array_keys($category));

        $list = array();
        $data = $cmsdb->getResults("SELECT `term_taxonomy_id`, COUNT(*) AS count FROM `wp_term_relationships`
        WHERE `term_taxonomy_id` IN ({$catStr}) GROUP BY `term_taxonomy_id`");

        foreach ($data as $d) {
            $list[] = array(
                'id' => intval($d->term_taxonomy_id),
                'name' => $category[$d->term_taxonomy_id],
                'count' => intval($d->count)
            );
        }

        return genOutput(1, array('category' => $list));
    }
}